local UIBillRankReward = UIBase.New(PanelResNames.UIBillRankReward)

local panel                 = UIBillRankReward

local m_rank_list = nil
local m_show_type = nil

function panel.Init(type)
    m_show_type = type
    UIBase.Init(UIBillRankReward)

end

function panel:OnCreate()
    self:RegisterEvent(Config.EventType.Billboard_rank_reddot_refresh, self.RefreshRedDot)
    self:RegisterEvent(Config.EventType.Billboard_rank_list, self.refreshList)

    self:RegisterOnClick(self:GetChild("BgClose"), self.onClick_closePanel)
    self:RegisterOnClick(self:GetChild("btn_getall"), self.onClick_receiveRewards)
    self:RegisterOnClick(self:GetChild("btnBack"), self.onClick_back)

    self.list_rankreward = self:GetChild("list_rankreward")
    self.ctr_get = self:GetController("get")
    self.list_rankreward.itemRenderer = panel.render_rankItem
    self.list_rankreward:SetVirtual()
    self.btn_getall = self:GetChild("btn_getall")
    

    self:SetDynamicList(self.list_rankreward, self.render_rankItem, nil, true)

    -- self:ShowList()
    logicMgr.BillboardMgr.req_RankServerRewardInfo(m_show_type)
end

function panel:ShowList()
    if not m_show_type then
        m_show_type = EnumConst.BillRankType.Stage
    end

    m_rank_list = logicMgr.BillboardMgr.GetRankList(m_show_type)
    if m_rank_list then
        self.list_rankreward.numItems = #m_rank_list
    end
end

function panel:render_rankItem(index, obj)
    if not m_rank_list[index] then
        return
    end
    local data = m_rank_list[index]
    local serverData = data.serverData
    local text_title = obj:GetChild("text_title")
    local ctr_type = obj:GetController("ctrl_type")
    local btn_item = obj:GetChild("btn_item")
    local btn_get = obj:GetChild("btn_get")
    local btn_head = obj:GetChild("btn_head")
    

    local playerName = obj:GetChild("text_playername")
    local txt_time = obj:GetChild("text_time")
    local icon = obj:GetChild("btn_head"):GetChild("head"):GetChild("HeadIcon")
    local frame = obj:GetChild("btn_head"):GetChild("frame")
    local rtext_check = obj:GetChild("rtext_check")

    if m_show_type == EnumConst.BillRankType.Stage then
        local p = data.cfg.f_Progress
        text_title.text = string.format(getLanguage("BillRankReward01"), math.floor(p / 100) .."-".. p % 100)
    elseif m_show_type == EnumConst.BillRankType.YongJie then
        text_title.text = string.format(getLanguage("BillRankReward03"),  data.cfg.f_Progress)
    elseif m_show_type == EnumConst.BillRankType.Relic then
        text_title.text = string.format(getLanguage("BillRankReward02"),  data.cfg.f_Progress)
    else
        local p = data.cfg.f_Progress
        text_title.text = string.format(getLanguage("BillRankReward04"),  math.floor(p / 100) .."-".. p % 100)
    end

    btn_get.onClick:Clear()
    btn_get.onClick:Add(function()
        logicMgr.BillboardMgr.req_GetRankServerReward(m_show_type, data.cfg.f_ID)
    end)

    rtext_check.onClick:Clear()
    rtext_check.onClick:Add(function()
        showUI(PanelNames.UIFivePlayer, {type = m_show_type, step = data.cfg.f_Progress})
    end)

    if data then
        local ctr_item = CtrlManager.GetCtrl(PanelNames.Item)
        local it = {itemModelId = data.cfg.f_Award[1][1], num = tonumber(data.cfg.f_Award[1][2])}
        ctr_item.initData(btn_item, it)

        if serverData then
            ctr_type.selectedIndex = serverData.isGot == 0 and 1 or 2
            playerName.text = serverData.playerInfo.name
            local tt = tonumber(tostring(serverData.playerInfo.time))
            txt_time.text = os.date("%Y-%m-%d", math.ceil(tt / 1000))
            self:SetPlayerIcon(icon, serverData.playerInfo.headIcon, frame, serverData.playerInfo.headFrame)
        else
            ctr_type.selectedIndex = 0
        end
    end
end

function panel:onClick_closePanel()
    closeUI(PanelNames.UIBillRankReward)
end

function panel:onClick_receiveRewards()
    BillboardMgr.req_GetRankServerReward(m_show_type, 0)
end

function panel:onClick_back()
    closeUI(PanelNames.UIBillRankReward)
end

function panel:refreshList()
    local tb = BillboardMgr.GetRankListByType(m_show_type)
    local list = tb.passList or {}

    m_rank_list = logicMgr.BillboardMgr.GetRankList(m_show_type)

    local tmp = {}
    local t
   
    local count = 0
    local have_reward = false
    for _,v in pairs(m_rank_list) do
        if v.f_Progress >= tb.max then
            t = {cfg = v, state = 2, bigger = true}
        else
            t = {cfg = v, state = 2, bigger = false}
        end
        
        for _,vv in pairs(list) do
            if vv.step == v.f_Progress then
                t.serverData = vv
                t.state = vv.isGot == 0 and 3 or 1
                if vv.isGot == 0 then
                    have_reward = true
                end
                break
            end
        end
        
        if t.state == 2 then
            if count < 3 and t.bigger then
                table.insert(tmp, t)
                count = count + 1
            end
        else
            table.insert(tmp, t)
        end
        
    end

    if have_reward or BillboardMgr.HaveOutSideReward(m_show_type) then
        panel.ctr_get.selectedIndex = 1
        panel.btn_getall.touchable = true
    else
        panel.ctr_get.selectedIndex = 0
        panel.btn_getall.touchable = false
    end

    table.sort(tmp, function(a,b)
        if a.state ~= b.state then
           return a.state > b.state
        elseif a.state == b.state and a.state ~= 2  then
            return a.cfg.f_ID > b.cfg.f_ID
        end
        return a.cfg.f_ID < b.cfg.f_ID
    end)
    m_rank_list = tmp

    if m_rank_list then
        panel.list_rankreward.numItems = #m_rank_list
    end
end

function panel:RefreshRedDot()
    local b = BillboardMgr.HaveOutSideReward(m_show_type)
    
    self.ctr_get.selectedIndex = b and 1 or 0
end

return UIBillRankReward